package com.example;

import java.util.Scanner;

/**
 * 模拟水果超市
 */
public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎进入水果商城");
        while (true) {
            /* 这部分提示信息每次循环都要执行 */
            System.out.println("请选择购买如下商品:");
            System.out.println("1:苹果");
            System.out.println("2:香蕉");
            System.out.println("3:西瓜");
            String fruit = ""; // 水果种类
            double price = 0.0; // 水果单价
            /* 用户输入序号 */
            if (scanner.hasNext()) {
                String num = scanner.next();
                // 1.要根据序号判断水果的种类
                // 2.要根据序号判断水果的单价
                switch (num) {
                    case "1":
                        fruit = "苹果";
                        price = 5.5;
                        break;
                    case "2":
                        fruit = "香蕉";
                        price = 6.6;
                        break;
                    case "3":
                        fruit = "西瓜";
                        price = 3.3;
                        break;
                    default:
                        System.out.println("输入有误，请重新选择");
                        continue;
                }
            }
            System.out.println("您选择的水果是:" + fruit + "，单价:" + price + "元/斤");
            System.out.println("===============");
            System.out.println("请输入购买的斤数(必须为数字):");
            /* 用户输入数量 */
            if (scanner.hasNextDouble()) {
                double count = scanner.nextDouble();
                /* 这里要用到fruit、price */
                /* 计算总价打印到控制台 */
                System.out.println("您购买的" + count + "斤" + fruit + "总价为:" + count * price + "元");
            }
            System.out.println("===============");
            System.out.println("请选择如下操作:");
            System.out.println("1:继续购物");
            System.out.println("2:结算退出");
            /* 用户输入，是否继续购物 */
            if (scanner.hasNext()) {
                String num = scanner.next();
                if (!"1".equals(num)) {
                    break;
                }
            }
        }
    }
}
